package com.syh.jn.scm.service.impl.supplier.statistics;

import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.jn.scm.domain.supplier.entity.base.goods.Goods;
import com.syh.jn.scm.domain.supplier.entity.base.goods.GoodsBrand;
import com.syh.jn.scm.domain.supplier.entity.base.goods.GoodsCategory;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsBrandReadRepository;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsCategoryReadRepository;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsReadRepository;
import com.syh.jn.scm.domain.supplier.repository.finance.archive.ArchiveReadRepository;
import com.syh.jn.scm.dto.supplier.statistics.sale.*;
import com.syh.jn.scm.dto.supplier.statistics.stock.InventoryReportVo;
import com.syh.jn.scm.infrastructure.repository.utils.MybatisTemplate;
import com.syh.jn.scm.service.api.supplier.statistics.SalesReportService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SalesReportServiceImpl implements SalesReportService {

  @Resource(name="archiveRepoMbImpl")
  private ArchiveReadRepository archiveReadRepository;
  /**
   * 按商品统计销售报表
   * @param params
   * @return
   */
  @Override
  public List<GoodsSalesReportVo> goodsSalesReport(Map<String, Object> params) {
//    return MybatisTemplate.selectList("saleReportQRepository.goodsSalesReport",params);
    return this.getSalesReport(params);
  }

  private List<GoodsSalesReportVo> getSalesReport(Map<String, Object> params) {
    GoodsReadRepository goodsReadRepository = (GoodsReadRepository) SpringContextUtils.getBean("goodsRepositoryImpl");
    //获取要分页统计的商品列表
    Integer startRow = (Integer) params.get("startRow");
    Integer pageSize = (Integer) params.get("pageSize");
    Long businessEntityId = (Long) params.get("businessEntityId");
    //报表统计开始时间 如果没有选择开始时间  默认是从最初开始查询
    String reportBeginTimeStamp = params.get("beginTime") == null ? "0" : (String)params.get("beginTime");
    //如果没有选择结束时间  默认到当前时间
    String reportEndTimeStamp = params.get("endTime") == null ? String.valueOf(System.currentTimeMillis()) : (String)params.get("endTime");
    Wrapper goodsWp = Wrapper.build();
    this.setQuery(goodsWp,params);
    List<Goods> goodsList = goodsReadRepository.findListPage(goodsWp,startRow,pageSize);
    Map<String,InventoryReportVo> inventoryReportVoMapTemp = StockInventoryUtils.getStockInventory(reportBeginTimeStamp,reportEndTimeStamp,businessEntityId);
    Map<Long,InventoryReportVo> inventoryReportVoMap = new HashMap<>();
    inventoryReportVoMapTemp.forEach((key,inventoryReportVo) -> {
      if (inventoryReportVoMap.get(inventoryReportVo.getGoodsId()) != null) {
        InventoryReportVo inventoryReportVo1 = inventoryReportVoMap.get(inventoryReportVo.getGoodsId());
        inventoryReportVo1.setSaleQuantity(inventoryReportVo1.getSaleQuantity().add(inventoryReportVo.getSaleQuantity()));
        inventoryReportVo1.setSaleAmount(inventoryReportVo1.getSaleAmount().add(inventoryReportVo.getSaleAmount()));
        inventoryReportVo1.setSaleRetQuantity(inventoryReportVo1.getSaleRetQuantity().add(inventoryReportVo.getSaleRetQuantity()));
        inventoryReportVo1.setSaleRetAmount(inventoryReportVo1.getSaleRetAmount().add(inventoryReportVo.getSaleRetAmount()));
        inventoryReportVo1.setSaleLossQuantity(inventoryReportVo1.getSaleLossQuantity().add(inventoryReportVo.getSaleLossQuantity()));
        inventoryReportVo1.setSaleLossAmount(inventoryReportVo1.getSaleLossAmount().add(inventoryReportVo.getSaleLossAmount()));
        inventoryReportVo1.setSaleRetLossQuantity(inventoryReportVo1.getSaleRetLossQuantity().add(inventoryReportVo.getSaleRetLossQuantity()));
        inventoryReportVo1.setSaleRetLossAmount(inventoryReportVo1.getSaleRetLossAmount().add(inventoryReportVo.getSaleRetLossAmount()));
        inventoryReportVo1.setSaleSpillQuantity(inventoryReportVo1.getSaleSpillQuantity().add(inventoryReportVo.getSaleSpillQuantity()));
        inventoryReportVo1.setSaleSpillAmount(inventoryReportVo1.getSaleSpillAmount().add(inventoryReportVo.getSaleSpillAmount()));
        inventoryReportVo1.setSaleRetSpillQuantity(inventoryReportVo1.getSaleRetSpillQuantity().add(inventoryReportVo.getSaleRetSpillQuantity()));
        inventoryReportVo1.setSaleRetSpillAmount(inventoryReportVo1.getSaleRetSpillAmount().add(inventoryReportVo.getSaleRetSpillAmount()));
      } else {
        inventoryReportVoMap.put(inventoryReportVo.getGoodsId(),inventoryReportVo);
      }
    });
    List<GoodsSalesReportVo> goodsSalesReportVos = new ArrayList<>();
    goodsList.forEach(goods -> {
      if (inventoryReportVoMap.get(goods.getId()) != null) {
        GoodsSalesReportVo goodsSalesReportVo = new GoodsSalesReportVo();
        InventoryReportVo inventoryReportVo = inventoryReportVoMap.get(goods.getId());
        goodsSalesReportVo.setGoodsId(goods.getId());
        goodsSalesReportVo.setGoodsCode(goods.getCode());
        goodsSalesReportVo.setGoodsName(goods.getName());
        goodsSalesReportVo.setGoodsSpecName(goods.getSpecName());
        goodsSalesReportVo.setGoodsUnits(goods.getUnits());
        goodsSalesReportVo.setSaleQuantity(inventoryReportVo.getSaleQuantity());
        goodsSalesReportVo.setSaleAmt(inventoryReportVo.getSaleAmount());
        goodsSalesReportVo.setSaleRetQuantity(inventoryReportVo.getSaleRetQuantity());
        goodsSalesReportVo.setSaleRetAmt(inventoryReportVo.getSaleRetAmount());
        goodsSalesReportVo.setLossQuantity(inventoryReportVo.getSaleLossQuantity().add(inventoryReportVo.getSaleRetLossQuantity()));
        goodsSalesReportVo.setLossAmt(inventoryReportVo.getSaleLossAmount().add(inventoryReportVo.getSaleRetLossAmount()));
        goodsSalesReportVo.setSpillQuantity(inventoryReportVo.getSaleSpillQuantity().add(inventoryReportVo.getSaleSpillQuantity()));
        goodsSalesReportVo.setSpillAmt(inventoryReportVo.getSaleSpillAmount().add(inventoryReportVo.getSaleSpillAmount()));

        //符号纠正
        goodsSalesReportVo.setSaleQuantity(goodsSalesReportVo.getSaleQuantity().negate());
        goodsSalesReportVo.setSaleAmt(goodsSalesReportVo.getSaleAmt().negate());
        goodsSalesReportVo.setLossQuantity(goodsSalesReportVo.getLossQuantity().negate());
        goodsSalesReportVo.setLossAmt(goodsSalesReportVo.getLossAmt().negate());
        goodsSalesReportVos.add(goodsSalesReportVo);
      }
    });
    return goodsSalesReportVos;
  }



  private void setQuery(Wrapper wrapper, Map<String,Object> params) {
    wrapper.append(Restrictions.eq("businessEntityId",(Long)params.get("businessEntityId")));
    if (params.get("goodsName") != null) {
      wrapper.append(Restrictions.like("name",(String)params.get("goodsName")));
    }
    if (params.get("goodsId") != null) {
      wrapper.append(Restrictions.eq("id",(String)params.get("goodsId")));
    }
    if (params.get("goodsCategoryId") != null) {
      wrapper.append(Restrictions.eq("id",(String)params.get("goodsCategoryId")));
    }
    if (params.get("goodsBrandId") != null) {
      wrapper.append(Restrictions.eq("id",(String)params.get("goodsBrandId")));
    }
  }

  /**
   * 按商品统计销售报表数量
   * @param params
   * @return
   */
  @Override
  public Integer goodsSalesReportCount(Map<String, Object> params) {
//    return MybatisTemplate.selectOne("saleReportQRepository.goodsSalesReportCount",params);
    GoodsReadRepository goodsReadRepository = (GoodsReadRepository) SpringContextUtils.getBean("goodsRepositoryImpl");
    Wrapper goodsWp = Wrapper.build();
    this.setQuery(goodsWp,params);
    return goodsReadRepository.getCount(goodsWp);
  }

  /**
   * 按下游客户统计销售报表
   * @param params
   * @return
   */
  @Override
  public List<CustomerSalesReportVo> customerSalesReport(Map<String, Object> params) {
    return MybatisTemplate.selectList("saleReportQRepository.customerSalesReport",params);
  }

  /**
   * 按下游客户统计销售报表数量
   * @param params
   * @return
   */
  @Override
  public Integer customerSalesReportCount(Map<String, Object> params) {
    return MybatisTemplate.selectOne("saleReportQRepository.customerSalesReportCount",params);
  }

  /**
   * 按商品分类统计销售报表
   * @param params
   * @return
   */
  @Override
  public List<GoodsCategorySalesReportVo> goodsCategorySalesReport(Map<String, Object> params) {
//    return MybatisTemplate.selectList("saleReportQRepository.goodsCategorySalesReport",params);
    return this.getGoodsCategorySalesReport(params);
  }

  private List<GoodsCategorySalesReportVo> getGoodsCategorySalesReport(Map<String, Object> params) {
    GoodsCategoryReadRepository goodsCategoryReadRepository = (GoodsCategoryReadRepository) SpringContextUtils.getBean("goodsCategoryRepositoryImpl");
    //获取要分页统计的商品列表
    Integer startRow = (Integer) params.get("startRow");
    Integer pageSize = (Integer) params.get("pageSize");
    Long businessEntityId = (Long) params.get("businessEntityId");
    //报表统计开始时间 如果没有选择开始时间  默认是从最初开始查询
    String reportBeginTimeStamp = params.get("beginTime") == null ? "0" : (String)params.get("beginTime");
    //如果没有选择结束时间  默认到当前时间
    String reportEndTimeStamp = params.get("endTime") == null ? String.valueOf(System.currentTimeMillis()) : (String)params.get("endTime");
    Wrapper goodsCategoryWp = Wrapper.build();
    this.setQuery(goodsCategoryWp,params);
    List<GoodsCategory> goodsCategoryList = goodsCategoryReadRepository.findListPage(goodsCategoryWp,startRow,pageSize);
    Map<String,InventoryReportVo> inventoryReportVoMapTemp = StockInventoryUtils.getStockInventory(reportBeginTimeStamp,reportEndTimeStamp,businessEntityId);
    Map<Long,InventoryReportVo> inventoryReportVoMap = new HashMap<>();
    //把获取到的InventoryReportVo数据按商品分类重新汇总成inventoryReportVoMap
    inventoryReportVoMapTemp.forEach((key,inventoryReportVo) -> {
      if (inventoryReportVoMap.get(inventoryReportVo.getGoodsCategoryId()) != null) {
        InventoryReportVo inventoryReportVo1 = inventoryReportVoMap.get(inventoryReportVo.getGoodsCategoryId());
        inventoryReportVo1.setSaleQuantity(inventoryReportVo1.getSaleQuantity().add(inventoryReportVo.getSaleQuantity()));
        inventoryReportVo1.setSaleAmount(inventoryReportVo1.getSaleAmount().add(inventoryReportVo.getSaleAmount()));
        inventoryReportVo1.setSaleRetQuantity(inventoryReportVo1.getSaleRetQuantity().add(inventoryReportVo.getSaleRetQuantity()));
        inventoryReportVo1.setSaleRetAmount(inventoryReportVo1.getSaleRetAmount().add(inventoryReportVo.getSaleRetAmount()));
        inventoryReportVo1.setSaleLossQuantity(inventoryReportVo1.getSaleLossQuantity().add(inventoryReportVo.getSaleLossQuantity()));
        inventoryReportVo1.setSaleLossAmount(inventoryReportVo1.getSaleLossAmount().add(inventoryReportVo.getSaleLossAmount()));
        inventoryReportVo1.setSaleRetLossQuantity(inventoryReportVo1.getSaleRetLossQuantity().add(inventoryReportVo.getSaleRetLossQuantity()));
        inventoryReportVo1.setSaleRetLossAmount(inventoryReportVo1.getSaleRetLossAmount().add(inventoryReportVo.getSaleRetLossAmount()));
        inventoryReportVo1.setSaleSpillQuantity(inventoryReportVo1.getSaleSpillQuantity().add(inventoryReportVo.getSaleSpillQuantity()));
        inventoryReportVo1.setSaleSpillAmount(inventoryReportVo1.getSaleSpillAmount().add(inventoryReportVo.getSaleSpillAmount()));
        inventoryReportVo1.setSaleRetSpillQuantity(inventoryReportVo1.getSaleRetSpillQuantity().add(inventoryReportVo.getSaleRetSpillQuantity()));
        inventoryReportVo1.setSaleRetSpillAmount(inventoryReportVo1.getSaleRetSpillAmount().add(inventoryReportVo.getSaleRetSpillAmount()));
      } else {
        inventoryReportVoMap.put(inventoryReportVo.getGoodsCategoryId(),inventoryReportVo);
      }
    });
    //用于存放计算时GoodsCategorySalesReportVo
    Map<Long,GoodsCategorySalesReportVo> goodsCategorySalesReportVoMap = new HashMap<>();
    //用于存放最后输入的GoodsCategorySalesReportVo
    List<GoodsCategorySalesReportVo> goodsCategorySalesReportVos = new ArrayList<>();
    goodsCategoryList.forEach(goodsCategory -> {
      GoodsCategorySalesReportVo goodsCategorySalesReportVo = new GoodsCategorySalesReportVo();
      goodsCategorySalesReportVo.setGoodsCategoryId(goodsCategory.getId());
      goodsCategorySalesReportVo.setGoodsCategoryCode(goodsCategory.getCode());
      goodsCategorySalesReportVo.setGoodsCategoryName(goodsCategory.getName());
      goodsCategorySalesReportVo.setGoodsCateLevel(goodsCategory.getCateLevel());
      goodsCategorySalesReportVo.setGoodsSupCategoryId(goodsCategory.getSupId());
      goodsCategorySalesReportVo.setSaleQuantity(BigDecimal.ZERO);
      goodsCategorySalesReportVo.setSaleAmt(BigDecimal.ZERO);
      goodsCategorySalesReportVo.setSaleRetQuantity(BigDecimal.ZERO);
      goodsCategorySalesReportVo.setSaleRetAmt(BigDecimal.ZERO);
      goodsCategorySalesReportVo.setLossQuantity(BigDecimal.ZERO);
      goodsCategorySalesReportVo.setLossAmt(BigDecimal.ZERO);
      goodsCategorySalesReportVo.setSpillQuantity(BigDecimal.ZERO);
      goodsCategorySalesReportVo.setSpillAmt(BigDecimal.ZERO);
      goodsCategorySalesReportVoMap.put(goodsCategory.getId(),goodsCategorySalesReportVo);
    });
    goodsCategoryList.forEach(goodsCategory -> {
      if (inventoryReportVoMap.get(goodsCategory.getId()) != null) {
        GoodsCategorySalesReportVo goodsCategorySalesReportVo = goodsCategorySalesReportVoMap.get(goodsCategory.getId());
        InventoryReportVo inventoryReportVo = inventoryReportVoMap.get(goodsCategory.getId());
        goodsCategorySalesReportVo.setGoodsCategoryId(goodsCategory.getId());
        goodsCategorySalesReportVo.setGoodsCategoryCode(goodsCategory.getCode());
        goodsCategorySalesReportVo.setGoodsCategoryName(goodsCategory.getName());
        goodsCategorySalesReportVo.setGoodsCateLevel(goodsCategory.getCateLevel());
        goodsCategorySalesReportVo.setSaleQuantity(goodsCategorySalesReportVo.getSaleQuantity().add(inventoryReportVo.getSaleQuantity()));
        goodsCategorySalesReportVo.setSaleAmt(goodsCategorySalesReportVo.getSaleAmt().add(inventoryReportVo.getSaleAmount()));
        goodsCategorySalesReportVo.setSaleRetQuantity(goodsCategorySalesReportVo.getSaleRetQuantity().add(inventoryReportVo.getSaleRetQuantity()));
        goodsCategorySalesReportVo.setSaleRetAmt(goodsCategorySalesReportVo.getSaleRetAmt().add(inventoryReportVo.getSaleRetAmount()));
        goodsCategorySalesReportVo.setLossQuantity(goodsCategorySalesReportVo.getLossQuantity().add(inventoryReportVo.getSaleLossQuantity().add(inventoryReportVo.getSaleRetLossQuantity())));
        goodsCategorySalesReportVo.setLossAmt(goodsCategorySalesReportVo.getLossAmt().add(inventoryReportVo.getSaleLossAmount().add(inventoryReportVo.getSaleRetLossAmount())));
        goodsCategorySalesReportVo.setSpillQuantity(goodsCategorySalesReportVo.getSpillQuantity().add(inventoryReportVo.getSaleSpillQuantity().add(inventoryReportVo.getSaleSpillQuantity())));
        goodsCategorySalesReportVo.setSpillAmt(goodsCategorySalesReportVo.getSpillAmt().add(inventoryReportVo.getSaleSpillAmount().add(inventoryReportVo.getSaleSpillAmount())));
        this.setSupGoodsCategorySalesReport(goodsCategory.getSupId(),goodsCategorySalesReportVoMap,inventoryReportVo);
      }
    });
    goodsCategoryList.forEach(goodsCategory -> {
      GoodsCategorySalesReportVo goodsCategorySalesReportVo = goodsCategorySalesReportVoMap.get(goodsCategory.getId());
      //纠正符号
      goodsCategorySalesReportVo.setSaleQuantity(goodsCategorySalesReportVo.getSaleQuantity().negate());
      goodsCategorySalesReportVo.setSaleAmt(goodsCategorySalesReportVo.getSaleAmt().negate());
      goodsCategorySalesReportVo.setLossQuantity(goodsCategorySalesReportVo.getLossQuantity().negate());
      goodsCategorySalesReportVo.setLossAmt(goodsCategorySalesReportVo.getLossAmt().negate());
      goodsCategorySalesReportVos.add(goodsCategorySalesReportVoMap.get(goodsCategory.getId()));
    });
    return goodsCategorySalesReportVos;
  }

  private void setSupGoodsCategorySalesReport(Long supId,Map<Long, GoodsCategorySalesReportVo> goodsCategorySalesReportVoMap,InventoryReportVo inventoryReportVo) {
    if (supId != null && goodsCategorySalesReportVoMap.get(supId) != null) {
      GoodsCategorySalesReportVo goodsCategorySalesReportVo = goodsCategorySalesReportVoMap.get(supId);
      goodsCategorySalesReportVo.setSaleQuantity(goodsCategorySalesReportVo.getSaleQuantity().add(inventoryReportVo.getSaleQuantity()));
      goodsCategorySalesReportVo.setSaleAmt(goodsCategorySalesReportVo.getSaleAmt().add(inventoryReportVo.getSaleAmount()));
      goodsCategorySalesReportVo.setSaleRetQuantity(goodsCategorySalesReportVo.getSaleRetQuantity().add(inventoryReportVo.getSaleRetQuantity()));
      goodsCategorySalesReportVo.setSaleRetAmt(goodsCategorySalesReportVo.getSaleRetAmt().add(inventoryReportVo.getSaleRetAmount()));
      goodsCategorySalesReportVo.setLossQuantity(goodsCategorySalesReportVo.getLossQuantity().add(inventoryReportVo.getSaleLossQuantity().add(inventoryReportVo.getSaleRetLossQuantity())));
      goodsCategorySalesReportVo.setLossAmt(goodsCategorySalesReportVo.getLossAmt().add(inventoryReportVo.getSaleLossAmount().add(inventoryReportVo.getSaleRetLossAmount())));
      goodsCategorySalesReportVo.setSpillQuantity(goodsCategorySalesReportVo.getSpillQuantity().add(inventoryReportVo.getSaleSpillQuantity().add(inventoryReportVo.getSaleSpillQuantity())));
      goodsCategorySalesReportVo.setSpillAmt(goodsCategorySalesReportVo.getSpillAmt().add(inventoryReportVo.getSaleSpillAmount().add(inventoryReportVo.getSaleSpillAmount())));
      this.setSupGoodsCategorySalesReport(goodsCategorySalesReportVo.getGoodsSupCategoryId(),goodsCategorySalesReportVoMap,inventoryReportVo);
    }
  }
  /**
   * 按商品分类统计销售报表数量
   * @param params
   * @return
   */
  @Override
  public Integer goodsCategorySalesReportCount(Map<String, Object> params) {
//    return MybatisTemplate.selectOne("saleReportQRepository.goodsCategorySalesReportCount",params);
    GoodsCategoryReadRepository goodsCategoryReadRepository = (GoodsCategoryReadRepository) SpringContextUtils.getBean("goodsCategoryRepositoryImpl");
    Wrapper goodsCategoryWp = Wrapper.build();
    this.setQuery(goodsCategoryWp,params);
    return goodsCategoryReadRepository.getCount(goodsCategoryWp);
  }

  /**
   * 按商品品牌统计销售报表
   * @param params
   * @return
   */
  @Override
  public List<GoodsBrandSalesReportVo> goodsBrandSalesReport(Map<String, Object> params) {
//    return MybatisTemplate.selectList("saleReportQRepository.goodsBrandSalesReport",params);
    return this.getGoodsBrandSalesReport(params);
  }

  private List<GoodsBrandSalesReportVo> getGoodsBrandSalesReport(Map<String, Object> params) {
    GoodsBrandReadRepository goodsBrandReadRepository = (GoodsBrandReadRepository) SpringContextUtils.getBean("goodsBrandRepositoryImpl");
    //获取要分页统计的商品列表
    Integer startRow = (Integer) params.get("startRow");
    Integer pageSize = (Integer) params.get("pageSize");
    Long businessEntityId = (Long) params.get("businessEntityId");
    //报表统计开始时间 如果没有选择开始时间  默认是从最初开始查询
    String reportBeginTimeStamp = params.get("beginTime") == null ? "0" : (String)params.get("beginTime");
    //如果没有选择结束时间  默认到当前时间
    String reportEndTimeStamp = params.get("endTime") == null ? String.valueOf(System.currentTimeMillis()) : (String)params.get("endTime");
    Wrapper goodsCategoryWp = Wrapper.build();
    this.setQuery(goodsCategoryWp,params);
    List<GoodsBrand> goodsBrandList = goodsBrandReadRepository.findListPage(goodsCategoryWp,startRow,pageSize);
    Map<String,InventoryReportVo> inventoryReportVoMapTemp = StockInventoryUtils.getStockInventory(reportBeginTimeStamp,reportEndTimeStamp,businessEntityId);
    Map<Long,InventoryReportVo> inventoryReportVoMap = new HashMap<>();
    //把获取到的InventoryReportVo数据按商品品牌重新汇总成inventoryReportVoMap
    inventoryReportVoMapTemp.forEach((key,inventoryReportVo) -> {
      if (inventoryReportVoMap.get(inventoryReportVo.getGoodsBrandId()) != null) {
        InventoryReportVo inventoryReportVo1 = inventoryReportVoMap.get(inventoryReportVo.getGoodsBrandId());
        inventoryReportVo1.setSaleQuantity(inventoryReportVo1.getSaleQuantity().add(inventoryReportVo.getSaleQuantity()));
        inventoryReportVo1.setSaleAmount(inventoryReportVo1.getSaleAmount().add(inventoryReportVo.getSaleAmount()));
        inventoryReportVo1.setSaleRetQuantity(inventoryReportVo1.getSaleRetQuantity().add(inventoryReportVo.getSaleRetQuantity()));
        inventoryReportVo1.setSaleRetAmount(inventoryReportVo1.getSaleRetAmount().add(inventoryReportVo.getSaleRetAmount()));
        inventoryReportVo1.setSaleLossQuantity(inventoryReportVo1.getSaleLossQuantity().add(inventoryReportVo.getSaleLossQuantity()));
        inventoryReportVo1.setSaleLossAmount(inventoryReportVo1.getSaleLossAmount().add(inventoryReportVo.getSaleLossAmount()));
        inventoryReportVo1.setSaleRetLossQuantity(inventoryReportVo1.getSaleRetLossQuantity().add(inventoryReportVo.getSaleRetLossQuantity()));
        inventoryReportVo1.setSaleRetLossAmount(inventoryReportVo1.getSaleRetLossAmount().add(inventoryReportVo.getSaleRetLossAmount()));
        inventoryReportVo1.setSaleSpillQuantity(inventoryReportVo1.getSaleSpillQuantity().add(inventoryReportVo.getSaleSpillQuantity()));
        inventoryReportVo1.setSaleSpillAmount(inventoryReportVo1.getSaleSpillAmount().add(inventoryReportVo.getSaleSpillAmount()));
        inventoryReportVo1.setSaleRetSpillQuantity(inventoryReportVo1.getSaleRetSpillQuantity().add(inventoryReportVo.getSaleRetSpillQuantity()));
        inventoryReportVo1.setSaleRetSpillAmount(inventoryReportVo1.getSaleRetSpillAmount().add(inventoryReportVo.getSaleRetSpillAmount()));
      } else {
        inventoryReportVoMap.put(inventoryReportVo.getGoodsBrandId(),inventoryReportVo);
      }
    });
    List<GoodsBrandSalesReportVo> goodsBrandSalesReportVos = new ArrayList<>();
    goodsBrandList.forEach(goodsBrand -> {
      GoodsBrandSalesReportVo goodsBrandSalesReportVo = new GoodsBrandSalesReportVo();
      goodsBrandSalesReportVo.setGoodsBrandId(goodsBrand.getId());
      goodsBrandSalesReportVo.setGoodsBrandName(goodsBrand.getName());
      goodsBrandSalesReportVo.setSaleQuantity(BigDecimal.ZERO);
      goodsBrandSalesReportVo.setSaleAmt(BigDecimal.ZERO);
      goodsBrandSalesReportVo.setSaleRetQuantity(BigDecimal.ZERO);
      goodsBrandSalesReportVo.setSaleRetAmt(BigDecimal.ZERO);
      goodsBrandSalesReportVo.setLossQuantity(BigDecimal.ZERO);
      goodsBrandSalesReportVo.setLossAmt(BigDecimal.ZERO);
      goodsBrandSalesReportVo.setSpillQuantity(BigDecimal.ZERO);
      goodsBrandSalesReportVo.setSpillAmt(BigDecimal.ZERO);

      //符号纠正
      goodsBrandSalesReportVo.setSaleQuantity(goodsBrandSalesReportVo.getSaleQuantity().negate());
      goodsBrandSalesReportVo.setSaleAmt(goodsBrandSalesReportVo.getSaleAmt().negate());
      goodsBrandSalesReportVo.setLossQuantity(goodsBrandSalesReportVo.getLossQuantity().negate());
      goodsBrandSalesReportVo.setLossAmt(goodsBrandSalesReportVo.getLossAmt().negate());
      goodsBrandSalesReportVos.add(goodsBrandSalesReportVo);
    });
    return goodsBrandSalesReportVos;
  }

  /**
   * 按商品品牌统计销售报表数量
   * @param params
   * @return
   */
  @Override
  public Integer goodsBrandSalesReportCount(Map<String, Object> params) {
//    return MybatisTemplate.selectOne("saleReportQRepository.goodsBrandSalesReportCount",params);
    GoodsBrandReadRepository goodsBrandReadRepository = (GoodsBrandReadRepository) SpringContextUtils.getBean("goodsBrandRepositoryImpl");
    Wrapper goodsCategoryWp = Wrapper.build();
    this.setQuery(goodsCategoryWp,params);
    return goodsBrandReadRepository.getCount(goodsCategoryWp);
  }

  /**
   * 销售成本报表统计
   * @param params
   * @return
   */
  @Override
  public List<SaleCostReportVo> saleCostReport(Map<String, Object> params) {
    return MybatisTemplate.selectList("saleCostReportQRepository.salesCostReport",params);
  }

  @Override
  public SaleCostReportVo saleCostReportById(Long id) {
    Map<String, Object> params = new HashMap<>();
    params.put("id",id);
    return MybatisTemplate.selectOne("saleCostReportQRepository.salesCostReportById",params);
  }

  /**
   * 销售成本报表统计数量
   * @param params
   * @return
   */
  @Override
  public Integer saleCostReportCount(Map<String, Object> params) {
    return MybatisTemplate.selectOne("saleCostReportQRepository.salesCostReportCount",params);
  }
}
